﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Core.Service
{
    public abstract class BaseService
    {
        /// <summary>
        /// Post Http
        /// </summary>
        public static string PostRequest(string Url, string FromData, string ContentType)
        {
            var Bytes = Encoding.UTF8.GetBytes(FromData);

            var Request = WebRequest.Create(Url);

            Request.Method = "POST";
            Request.ContentType = ContentType;
            Request.ContentLength = Bytes.Length;

            using (var RequestStream = Request.GetRequestStream())
            {
                RequestStream.Write(Bytes, 0, Bytes.Length);
            }

            var Response = Request.GetResponse();

            using (var ResponseStream = Response.GetResponseStream())
            {
                using (var TextReader = new StreamReader(ResponseStream, Encoding.UTF8))
                {
                    return TextReader.ReadToEnd();
                }
            }
        }

        /// <summary>
        /// Get Http
        /// </summary>
        public static string GetRequest(string Url)
        {
            var Request = WebRequest.Create(Url);

            Request.Method = "GET";

            var Response = Request.GetResponse();

            using (var ResponseStream = Response.GetResponseStream())
            {
                using (var TextReader = new StreamReader(ResponseStream, Encoding.UTF8))
                {
                    return TextReader.ReadToEnd();
                }
            }
        }
    }
}